var { Action } = require("../../base/index");
var {
  clickUtils,
  ocrUtils,
  textUtils,
  randomUtils,
  swipeUtils,
  timeUtils,
  logUtils,
  clickContent,
  existContent,
} = require("../../utils/index");
var aRead = new Action("刷视频", {});
aRead.watchVideo = function (count) {
  var span = randomUtils.getRandom(8, 28);
  logUtils.info("将看第", count, "个视频，预计：", span, "秒");
  this.watchAndCheck(span);
  logUtils.info("将滑动，看下一个视频了");
  swipeUtils.swipeVideo();
};
aRead.watchAndCheck = function (span) {
  var startTime = Date.now();
  while (true) {
    sleep(randomUtils.getRandom(3, 5, 1000));
    var contents = ["点击进入", "直播间"];
    var TextOCR = ocrUtils.captureExistsOneText(contents);
    if (TextOCR) {
      return true;
    }
    var TextUI = textUtils.existsOneTextEndsWith(contents);
    if (TextUI) {
      return true;
    }
    var TextUI = textUtils.existsOneTextStartsWith(contents);
    if (TextUI) {
      return true;
    }
    var hasExec = timeUtils.hasExec(startTime);
    if (hasExec >= span) {
      return;
    }
    if (randomUtils.possible(80, 100)) {
      return;
    }
  }
};
aRead.exec = function () {
  var content = "点击翻倍";
  if (clickContent(content)) {
    sleep(randomUtils.getRandom(1.5, 3, 1000));
  }
  var baseSpan = 60;
  var span = randomUtils.getRandom(baseSpan, baseSpan + 30);
  logUtils.info("本轮将看视频：", span, "秒");
  var startTime = Date.now();
  var count = 0;
  while (true) {
    count++;
    this.watchVideo(count);
    var hasExec = timeUtils.hasExec(startTime);
    logUtils.info("本轮已经看了", hasExec, "/", span);
    if (hasExec >= span) {
      logUtils.info("结束", this.name);
      break;
    }
  }
  var content = "点击重播";
  if (existContent(content)) {
    swipeUtils.swipeVideo();
    sleep(randomUtils.getRandom(2, 5, 1000));
  }
};

module.exports = aRead;
